package com.jy.appmarket.utils;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.v4.util.LruCache;

public class MemoryCache {
	private LruCache<Integer, Bitmap> cache = new LruCache<Integer, Bitmap>(1024 * 1024 * 6) {
		@SuppressLint("NewApi")
		protected int sizeOf(Integer key, Bitmap value) {
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
				return value.getAllocationByteCount();
			} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
				return value.getByteCount();
			} else {
				return value.getRowBytes() * value.getHeight();
			}
		}
	};

	public Bitmap get(int hashCode) {
		return cache.get(hashCode);
	}

	public void put(int hashCode, Bitmap bmp) {
		if (bmp != null) {
			cache.put(hashCode, bmp);
		}
	}

	public void clear() {
		cache.evictAll();
	}
}